import 'package:flutter/material.dart';
import 'package:flutter_yres/model/shop/pdd_cate_model.dart';
import './pdd_sub_page.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/pages/shop/SearchGoods.dart';
import './newPddHome.dart';
class NewPddPage extends StatefulWidget {
  NewPddPage({Key key}) : super(key: key);

  @override
  _NewPddPageState createState() => _NewPddPageState();
}

class _NewPddPageState extends State<NewPddPage> {
    TabController _tabController;
  List<PddCateModelData> tabs = []; //拼多多顶级分类数组

  @override
  Widget build(BuildContext context) {
        return new DefaultTabController(
      length: tabs.length,
      child:  Scaffold(
        appBar:  AppBar(
          backgroundColor: Colors.red,
          title: Text('拼多多'),
          actions: <Widget>[
            new IconButton(
                      icon: new Icon(Icons.search, color: Colors.white),
                      iconSize:30.0 ,
                      onPressed: () {
                        Navigator.of(context)
                                .push(new MaterialPageRoute(builder: (context) {
                              return new SearchGoodsPage(type:'2');
                            }));
                      },
            )
          ],
          bottom:  TabBar(
            isScrollable: true,
            tabs: tabs.map((item) {
              return _tabsBuilder(context, item);
            }).toList(),
            controller: _tabController,
            onTap: (index) {
              print(tabs[index].pdd_id);
            },
          ),
        ),
        body: TabBarView(
          children: tabs.map<Widget>((item) {
            print(item.name);
            // print(item.pdd_cat_id);
            print(item.pdd_id);
            if(item.name=='精选'){
return NewPddHomePage();
            }else{
              return PddSubPage(pdd_id: item.pdd_id,pdd_cat_id:item.pdd_cat_id);
            }
          }).toList(),
        ),
      ),
    );
  }
    Widget _tabsBuilder(BuildContext context, item) {
    return Tab(text: item.name);
  }

  @override
  void initState() {
    getPDDTopCatList();
    super.initState();
  }


  /*拼多多顶级列表*/
  getPDDTopCatList() async {
    FormData data = new FormData.fromMap({});
    Response<String> response =
        await HttpUtil().post(Api.GetPddTopCatListUrl, data: data);
    Map userMap = json.decode(response.data);
    // print(userMap);
    List list = userMap['data']['list'];
    Map map =new Map<String, dynamic>.from({'pdd_cat_id': '666888', 'name': '精选', 'icon': '/Public/Upload/PddCat/5e6ca6ec55a67474.png', 'sort': '0', 'is_show': 'Y', 'pid': '0', 'pdd_id': '666888'}); 
    // list.add(map);
    list.insert(0, map);
    // print(list);
    userMap['data']['list'] = list;
    //  print(userMap);
    PddCateModelEntity itemEntity = new PddCateModelEntity.fromJson(userMap);
    if (itemEntity.code == 1) {
      if (mounted) {
        setState(() {
          tabs.clear();
          tabs = itemEntity.data;
        });
      }
    }
  }
}